<?php
/**
 * @file guise.php $
 * @author LiangJian (liangjian@haibao.com) $
 * @date 2015年9月22日 上午11:08:09 $
 * @brief 绑定马甲用户 $
 */
namespace haibao\cms\business;

class Guise extends \haibao\cms\business\Base{
    
    /**
     * @var $_instance
     */
    private static $_instance;
    
    /**
     * instance()
     */
    private static function instance(){
        if(!self::$_instance){
            self::$_instance = new \haibao\cms\data\Guise();
        }
        return self::$_instance;
    }
    
    public static function getGuiseById($guiseId){
        return self::instance()->getGuiseById($guiseId);
    }
    
    public static function checkBind($guiseId){
        $guise = self::instance()->getGuiseByCondition(self::getCurrentUser()->Id, $guiseId);
        return $guise != null;
    }
    
    public static function getGuiseListByUserId($userId){
        return self::instance()->getGuiseListByUserId($userId);
    }
    public static function getGuiseNameByUserId($userIds){
        return self::instance()->getGuiseNameByUserId($userIds);
    }
    /**
     * @param string $username
     * @param string $password
     */
    public static function saveGuiseByUsername($username, $password){
        if(!$username){
            throw new BusinessException('用户名不能为空！');
        }
        if(!$password){
            throw new BusinessException('密码不能为空！');
        }
        
        if(self::isMobile($username)){
        	$user = \haibao\user\business\User::getUserByMobile($username);
        } else {
        	$user = \haibao\user\business\User::getUserByUsername($username);
        }
        if(!$user){
            throw new BusinessException('用户名“'.$username.'”没有注册过！');
        }
        if($user && self::checkBind($user['Id'])){
            throw new BusinessException('已经绑定过该用户！');
        }
        
        if(!self::checkPassword($password, $user['Password'])){
            throw new BusinessException('密码输入错误！');
        }
        $model = new \haibao\cms\model\data\Guise();
        $model->Username = $user['UserName'];
        $model->Nickname = $user['NickName'];
        $model->UserId = self::getCurrentUser()->Id;
        $model->GuiseId = $user['Id'];
        $model->CreateTime = new \DateTime();
        self::instance()->addGuise($model);
    }
    
    /**
     * @param int $guiseId
     */
    public static function saveGuise($guiseId, $nickname, $username){
        if(!$nickname){
            throw new BusinessException('用户昵称不能为空！');
        }
        if(!$username){
            throw new BusinessException('用户名不能为空！');
        }
        if(self::checkBind($guiseId)){
            throw new BusinessException('已经绑定过该用户！');
        }
        
        $model = new \haibao\cms\model\data\Guise();
        $model->Username = $username;
        $model->Nickname = $nickname;
        $model->UserId = self::getCurrentUser()->Id;
        $model->GuiseId = $guiseId;
        $model->CreateTime = new \DateTime();
        
        self::instance()->addGuise($model);
    }
    
    /**
     * 查询当前登录用户绑定的马甲
     */
    public static function getGuiseList(){
        $user = array();
        $userId = self::getCurrentUser()->Id;
        $userList = self::instance()->getGuiseListByUserId($userId);
        if($userList){
            $userIds = array();
            foreach($userList as $value){
                array_push($userIds, $value['GuiseId']);
            }
            $user = self::instance()->getGuiseByUserIds($userIds);
        }
        return $userList;
    }
    
    public static function getUserByName($name){
        return self::instance()->getUserByName($name);
    }
    
    public static function deleteGuise($id){
        return self::instance()->deleteGuise((int)$id);
    }

    public static function getAllGuise(){
        $filter = new \haibao\cms\model\filter\Guise();
        $filter->select(array($filter::CONDITION_FIELD_NAME_GUISE_ID));
        $datas = self::instance()->getAll($filter);
        $guiseIds = array();
        foreach($datas as $data){
            $guiseIds[] = $data->GuiseId;
        }
        return $guiseIds;
    }
    
    public static function getAllGuiseInfoList(){
        $filter = new \haibao\cms\model\filter\Guise();
        $filter->select(array(
            $filter::CONDITION_FIELD_NAME_GUISE_ID,
            $filter::CONDITION_FIELD_NAME_NICK_NAME
        ));
        $datas = self::instance()->getAll($filter);
        $guiseIds = array();
        foreach($datas as $data){
            array_push($guiseIds, array('id'=>$data->GuiseId,'name'=>$data->Nickname));
        }
        return $guiseIds;
    }
    public static function getAppAdminInfoList(){
         return self::instance()->getAppAdminInfoList();
    }
    private static function checkPassword($password, $dbPassword){
		$pwd_prefix = explode('$', $dbPassword);
		$password = $pwd_prefix[1] . $password;
		$password = 'sha1$' . $pwd_prefix[1] . '$' . sha1($password);
		return $password == $dbPassword;
	}
	
	private static function isMobile($mobile){
		return preg_match("/^1[3|4|5|7|8][0-9]\d{8,8}$/", $mobile);
	}
    
}